package com.sogoodsoft.web.crm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.common.SysTypeUIAction;

public class GoodsCateAction extends CrudActionSupport < SysClass > {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private SysClass entity;

    private Long id;

    private SysClass parent;

    private Long parentId;

    private String ul;

    public final static String GOODS_CATE_CODE = "GOODS_CATEGORY";

    @Autowired
    private SysClassService ser;

    @Override
    public String delete() throws Exception {
        if (id != null) {
            entity = ser.get(id);
            if (!GOODS_CATE_CODE.equals(entity.getCode())) {
                ser.delete(entity);
                Struts2Utils.renderJson(DwzMsg.success());
            } else {
                Struts2Utils.renderJson(DwzMsg.fail("对不起，不能删根节点！"));
            }

        }

        return null;
    }

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    @Override
    public String list() throws Exception {
        SysTypeUIAction ui = new SysTypeUIAction();
        ul = ui.ulComposite(ser.getByCode(GOODS_CATE_CODE));
        return SUCCESS;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = ser.get(id);
        } else {
            entity = new SysClass();
        }

        if (parentId != null) {
            parent = ser.get(parentId);
            entity.setParent(parent);
        }
    }

    @Override
    public String save() throws Exception {
        ser.save(entity);
        Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
        return null;
    }

    @Override
    public SysClass getModel() {
        return entity;
    }

    public SysClass getEntity() {
        return entity;
    }

    public void setEntity(SysClass entity) {
        this.entity = entity;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUl() {
        return ul;
    }

    public void setUl(String ul) {
        this.ul = ul;
    }

    public SysClass getParent() {
        return parent;
    }

    public void setParent(SysClass parent) {
        this.parent = parent;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

}
